<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\Api\controller;
use app\common\controller\Api;

class User extends Api {


	public function getuser($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_log3.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data . "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $url = 'yyh2018';
        $token = md5($url);
        if ($token == $map->token) {
            if (isset($map->id)) {
                $result = model('user')->where('uid', $map->id)->find();
                if ($result) {
                    $this->json_success($result);
                } else {
                    $this->json_error(306);
                }
            }
        } else {
            $this->json_error(304,'token');
        }
	}
    public function adduser($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_log1.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data .  "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $url = 'yyh2018';
        $token = md5($url);
        if ($token == $map->token) {
            $map = $this->setuser($map);
            if ($map) {
                $result = model('user')->validate(true)->save($map);
                if ($result) {
                    $this->json_success();
                } else {
                    $this->json_error(306);
                }
            }
        } else {
            $this->json_error(304,'token');
        }
    }

    public function edituser($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_log2.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data . "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $url = 'yyh2018';
        $token = md5($url);
        if ($token == $map->token) {
            $map = $this->setuser($map);
            if ($map) {
                $result = model('user')->validate('member.edit')->save($map, array('uid'=>$map['uid']));
                if ($result) {
                    $this->json_success();
                } else {
                    $this->json_error(306);
                }
            }
        } else {
            $this->json_error(304,'token');
        }
    }

    public function getbusiness($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_log4.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data . "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $url = 'yyh2018';
        $token = md5($url);
        if ($token == $map->token) {
            $result = model('user')->where('is_shop', 1)->field('uid,username,business_name')->select();
            if ($result) {
                $this->json_success($result);
            } else {
                $this->json_error(306);
            }
        } else {
            $this->json_error(304,'token');
        }
    }

    public function login($data){
        header("Content-Type:text/html;charset=utf-8");
        $fp = fopen("log/show_log4.txt", "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\r\n" . $data . "\r\n");
        flock($fp, LOCK_UN);
        fclose($fp);
        $map=json_decode($data);
        $url = 'yyh2018';
        $token = md5($url);
        if ($token == $map->token) {
            if (!isset($map->username) || !$map->username) {
                $this->data['msg'] = "用户名不能为空！";
                return $this->data;
            }
            if (!isset($map->password) || !$map->password) {
                $this->data['msg'] = "密码不能为空！";
                return $this->data;
            }

            $user = model('User')->field('uid,username,password,salt')->where('username', $this->request->post('username'))->find();
            if ($user['password'] === md5($map->password.$user['salt'])) {
                $this->data['code'] = 1;
                $user['access_token'] = authcode($user['uid'].'|'.$user['username'].'|'.$user['password'], 'ENCODE');
                $this->data['data'] = $user;
            }else{
                $this->data['msg'] = "密码错误！";
            }
            return $this->data;
        } else {
            $this->json_error(304,'token');
        }
    }
}